import Request from './request';
import {Cache} from '@/core';
export default function http(config) {
    let httpConfig = null;
    return {
        create(url, data) {
            if (!httpConfig) {
                let {appConfig} = config();
                httpConfig = appConfig.http;
            }

            return new Request(url, data, httpConfig).setToken(Cache.getToken());
        },

        /**
         * get 请求
         * @param url
         * @param params
         */
        get(url, params) {
            return this.create(url, params).setMethod("get");
        },

        /**
         * post 请求
         * @param url
         * @param data
         */
        post(url, data) {
            return this.create(url, data).setMethod('post');
        },

        /**
         * put 请求
         * @param url
         * @param data
         */
        put(url, data) {
            return this.create(url, data).setMethod("put");
        },

        /**
         * delete请求
         * @param url
         * @param params
         */
        delete(url, params) {
            return this.create(url, params).setMethod("delete");
        },

		/**
		 * 上传
		 * @param file
		 * @param data
		 * @param url
		 */
		upload(file, data, url) {

		    url = url ? url : httpConfig.uploadUrl;
		    let formData = {
		        ...data,
		        file: file
		    }

		    return this.create(url, formData).setMethod("upload");
		}
    }
}
